﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace IceCream.icemodel
{
    /// <summary>
    /// 咖啡机数据调用
    /// </summary>
    public class BaseService
    {
        public static string url_start = "http://jaka.motonbackend.top/swoft/api/motong/";
        // public static string url_start = "http://192.168.10.99:86/swoft/api/motong/";
        private static BaseService instance;
        private BaseService() { }
        public static BaseService Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new BaseService();
                }
                return instance;
            }
        }
        public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {   // 总是接受  
            return true;
        }
        public string ServerGet(string url)
        {
            string result = string.Empty;
            try
            {
                url = url_start + url;
                HttpWebRequest request = null;
                request = WebRequest.Create(url) as HttpWebRequest;
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                request.ProtocolVersion = HttpVersion.Version11;
                // 这里设置了协议类型。
                ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; ;// SecurityProtocolType.Tls1.2;
                request.KeepAlive = false;
                ServicePointManager.CheckCertificateRevocationList = true;
                ServicePointManager.DefaultConnectionLimit = 100;
                ServicePointManager.Expect100Continue = false;

                request.Method = "GET"; //使用get方式发送数据
                request.ContentType = "application/json";
                request.Accept = "application/json";
                request.Referer = null;
                request.AllowAutoRedirect = true;

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();

                using (StreamReader sr = new StreamReader(stream))
                {
                    result = sr.ReadToEnd();
                }
                //Console.Write(result);
                return result;
            }
            catch (Exception)
            {
                return result;
            }
        }
        private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
        {
            return true;
        }
        public string HttpPostRaw(string url, string data, bool ismake = false)
        {
            string value = "";
            try
            {
                url = url_start + url;
                HttpWebRequest reqest = (HttpWebRequest)WebRequest.Create(url);
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                reqest.ProtocolVersion = HttpVersion.Version11;
                // 这里设置了协议类型。
                ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; ;// SecurityProtocolType.Tls1.2;
                reqest.KeepAlive = false;
                ServicePointManager.CheckCertificateRevocationList = true;
                ServicePointManager.DefaultConnectionLimit = 100;
                ServicePointManager.Expect100Continue = false;
                reqest.Method = "POST";
                reqest.ContentType = "application/json";
                Stream stream = reqest.GetRequestStream();
                byte[] bs = System.Text.Encoding.UTF8.GetBytes(data);
                stream.Write(bs, 0, bs.Length);
                stream.Flush();
                stream.Close();
                if (ismake)
                    reqest.Timeout = 1000;
                ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
                HttpWebResponse response = (HttpWebResponse)reqest.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                value = sr.ReadToEnd();
                response.Close();
                return value;
            }
            catch (Exception ex)
            {
                // error = ex.ToString();
                return "";
            }
        }
        public void HttpPost(string Url, string postDataStr)
        {
            string realurl = url_start + Url;
            CoffeeProductID ceshi = new CoffeeProductID();
            ceshi.productId = postDataStr;
            string json = JsonConvert.SerializeObject(ceshi);
            HttpPostRaw(realurl, json);

        }
    }
    /// <summary>
    /// 制作饮品ID
    /// </summary>
    public class CoffeeProductID
    {
        public string productId { get; set; }
    }
    /// <summary>
    /// 咖啡产品ID
    /// </summary>
    public class CoffeeProductIDs
    {
        /// <summary>
        /// 美式
        /// </summary>
        public static string americano = "3a97b25c-ebb1-4f7c-11ef-08d74d909a0a";
        /// <summary>
        /// 冰美式
        /// </summary>
        public static string iceamericano = "312a5a2c-8240-44c1-8901-08d8dd936e2e";
        /// <summary>
        /// 拿铁
        /// </summary>
        public static string latte = "3b63d84d-6942-4575-6dc5-08d75d18a869";
        /// <summary>
        /// 卡布奇诺
        /// </summary>
        public static string cappuccino = "26c145b4-9466-4149-2d42-08d75dad4154";
        /// <summary>
        /// 意式
        /// </summary>
        public static string espresso = "3564a43a-a753-4a5c-bb9f-7a9e8c383a73";
        /// <summary>
        /// 巧克力
        /// </summary>
        public static string chocolate = "f8831a02-86a4-481f-8de4-386c52742d58";
        /// <summary>
        /// 冰拿铁
        /// </summary>
        public static string icelatte = "312a5a2c-8240-44c1-8901-08d8dd936e2e";
        /// <summary>
        /// 热牛奶天鹅
        /// </summary>
        public static string hotmilk = "7d35bac4-cdfd-471b-8900-08d8dd936e2e";
        /// <summary>
        /// 热牛奶叶子
        /// </summary>
        public static string hotmilk1 = "ea9281f2-56b3-4c2a-a1a1-74245671e338";
        /// <summary>
        /// 热牛奶郁金香
        /// </summary>
        public static string hotmilk2 = "4ff24a21-d5b8-4c20-890c-38992a7d33b0";
        /// <summary>
        ///巧克力牛奶
        /// /// </summary>
        public static string hotmilk4 = "6048931c-a07a-4d50-b16e-199cef479f2d";
        /// <summary>
        /// 热牛奶打印
        /// </summary>
        public static string hotmilk3 = "73991ffd-cb92-4a0f-a646-fda005e2c7ea";
        /// <summary>
        /// 热巧
        /// </summary>
        public static string hotchocolate = "312a5a2c-8240-44c1-8901-08d8dd936e2e";
        /// <summary>
        /// 澳式白咖啡
        /// </summary>
        public static string AustralianStyleWhite = "32d8d4a1-837e-403a-8e1c-16c4bc572211";
        /// <summary>
        /// 玛奇朵咖啡
        /// </summary>
        public static string Macchiato = "38fd2278-78ab-4582-78e6-08db46f97a01";
    }
    public class HttpMsg
    {
        private static HttpMsg instance;
        private HttpMsg() { }

        public static HttpMsg Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new HttpMsg();
                }
                return instance;
            }
        }
        /// <summary>
        /// 获取库存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string getDeviceStockMsg(string data)
        {
            try
            {
                var Stock = BaseService.Instance.HttpPostRaw("getDeviceStock", data);
                return Stock;
            }
            catch (Exception)
            {
                return "";
            }
        }
        /// <summary>
        /// 保存物料和设备状态
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string saveDeviceMatterMsg(string data)
        {
            try
            {
                var Matter = BaseService.Instance.HttpPostRaw("saveDeviceMatter", data);
                return Matter;
            }
            catch (Exception)
            {
                return "";
            }
        }
        /// <summary>
        /// 所有排队订单信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string deviceOrderQueueListMsg(string data)
        {
            try
            {
                var QueueList = BaseService.Instance.HttpPostRaw("deviceOrderQueueList", data);
                return QueueList;
            }
            catch (Exception)
            {
                return "";
            }
        }
        /// <summary>
        /// 修改订单状态
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string editDeviceOrderStatusMsg(string data)
        {
            try
            {
                var editDevice = BaseService.Instance.HttpPostRaw("editDeviceOrderStatus", data);
                return editDevice;
            }
            catch (Exception)
            {
                return "";
            }
        }
        /// <summary>
        /// 扫码排队
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string orderQueueMsg(string data)
        {
            try
            {
                var Queue = BaseService.Instance.HttpPostRaw("orderQueue", data);
                return Queue;
            }
            catch (Exception)
            {
                return "";
            }
        }
    }
    public class HTTPMsg
    {
        public string code { get; set; }
        public string msg { get; set; }
        public object data { get; set; }
    }
    public class deviceOrderQueueList
    {
        public string id { get; set; }
        public string num { get; set; }
        public string realPrice { get; set; }
        public string status { get; set; }
        public string orderNum { get; set; }
        public string created_at { get; set; }
        public string createdAt { get; set; }
        public string statusName { get; set; }
        public List<DeviceOrderQueue> typeList4 { get; set; }
        public List<DeviceOrderQueue> typeList3 { get; set; }
        public List<DeviceOrderQueue> typeList2 { get; set; }
        public List<DeviceOrderQueue> typeList1 { get; set; }
    }
    public class DeviceOrderQueue
    {

        public int id { get; set; } = 0;
        public string type { get; set; } = "";
        public int orderId { get; set; } = 0;
        public string goodsId { get; set; } = "";
        /// <summary>
        /// 名称
        /// </summary>
        public string goodsName { get; set; } = "";
        public string goodsImg { get; set; } = "";
        public string lhImgPath { get; set; } = "";
        public string goodsOptionName { get; set; } = "";
        /// <summary>
        /// -2后台删除 -1已删除 0已取消 1待付款 2已付款 3扫码排队(待制作) 4制作中 5已完成 -3已退款
        /// </summary>
        public string status { get; set; } = "";
        public string price { get; set; } = "";
        public string rePrice { get; set; } = "";
        public string num { get; set; } = "";
        public string totalPrice { get; set; } = "";
        /// <summary>
        /// 小吃机出餐内容
        /// </summary>
        public string jsonCodeVal { get; set; } = "";
        public string path { get; set; } = "";
        /// <summary>
        /// 图片
        /// </summary>
        public string goodsPath { get; set; } = "";
        public string statusName { get; set; } = "";
    }
    public class specification
    {
        /// <summary>
        /// 品类
        /// </summary>
        public string classCode { get; set; } = "";
        /// <summary>
        /// 温度 冰 1 正常 2
        /// </summary>
        public string headCode { get; set; } = "";
        /// <summary>
        /// 杯型 1，4oz 2，8oz 3，12oz 4，16oz
        /// </summary>
        public string cupCode { get; set; } = "";
        /// <summary>
        /// 甜度 1，全糖 2，七分糖 3，五分糖
        /// </summary>
        public string sweetCode { get; set; } = "";
        /// <summary>
        /// 糖类 1，糖1 2，糖2 3，糖3 4，糖4
        /// </summary>
        public string sugarCode { get; set; } = "";
        //[{\"classCode\":\"2101\"},{\"GJCode\":\"0\"},{\"XLCode\":\"0\"}]
        /// <summary>
        /// 酱料 0：不需要，1：酱料1，2：酱料2；3：酱料3 4：酱料1+2 5：酱料1+3 6：酱料2+3 7：酱料1+2+3
        /// </summary>
        public string GJCode { get; set; } = "0";
        /// <summary>
        /// 0：不需要， 1：果仁1，2：果仁2；3：果仁3 4：果仁1+2 5：果仁1+3 6：果仁2+3 7：果仁1+2+3
        /// </summary>
        public string XLCode { get; set; } = "0";
    }
    public class Device
    {
        public string deviceId { get; set; } = "3";
    }
    public class EditDeviceOrderStatus
    {
        /// <summary>
        /// 
        /// </summary>
        public int orderId { get; set; }
        public int orderGoodsId { get; set; }
        /// <summary>
        /// 4，制作中 5，已完成
        /// </summary>
        public string status { get; set; }
    }
}
